package com.spr.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spr.exception.FacultyNotFound;
import com.spr.model.Faculty;
import com.spr.repository.FacultyRepository;


@Service
public class FacultyServiceImpl implements FacultyService {

	@Resource
    private FacultyRepository FacultyRepository;
	 
	@Override
   @Transactional
   public Faculty create(Faculty Faculty) {
           Faculty createdFaculty = Faculty;
           return FacultyRepository.save(createdFaculty);
   }   
	
	@Override
   @Transactional
   public Faculty findById(int id) {
           return FacultyRepository.findOne(id);
   }
	
	 @Override
    @Transactional(rollbackFor=FacultyNotFound.class)
    public Faculty delete(int id) throws FacultyNotFound {
            Faculty deletedFaculty = FacultyRepository.findOne(id);
            
            if (deletedFaculty == null)
                    throw new FacultyNotFound();
            
            FacultyRepository.delete(deletedFaculty);
            return deletedFaculty;
    }

    @Override
    @Transactional
    public List<Faculty> findAll() {
            return FacultyRepository.findAll();
    }

    @Override
    @Transactional(rollbackFor=FacultyNotFound.class)
    public Faculty update(Faculty Faculty) throws FacultyNotFound {
            Faculty updatedFaculty = FacultyRepository.findOne(Faculty.getFacultyID());
            
            if (updatedFaculty == null)
                    throw new FacultyNotFound();
            
            updatedFaculty.setFirstName(Faculty.getFirstName());
            updatedFaculty.setLastName(Faculty.getLastName());
            updatedFaculty.setEmail(Faculty.getEmail());
            updatedFaculty.setDateOfBirth(Faculty.getDateOfBirth());
            updatedFaculty.setGender(Faculty.getGender());
            updatedFaculty.setDepartment(Faculty.getDepartment());
            updatedFaculty.setTitle(Faculty.getTitle());
            return updatedFaculty;
    }

}
